package com.xxxx.server;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CodeGenerator {

    /**
     * 数据源配置
     */
    private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder("jdbc:mysql://localhost:3306/yeb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai", "root", "root");

    /**
     * 处理all的情况
     * @param tables
     * @return
     */
    private static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }

    public static void main(String[] args) {
        //获取当前项目的路径
        String projectPath = System.getProperty("user.dir");

        FastAutoGenerator.create(DATA_SOURCE_CONFIG)
                .globalConfig(builder -> {
                    builder.author("veo")
                            .fileOverride()
                            .disableOpenDir()
                            .enableSwagger()
                            .outputDir(projectPath + "\\yeb-generator\\src\\main\\java");
                })
                .packageConfig(builder -> {
                    builder.parent("com.xxxx.generator")
                            .entity("pojo")
                            .mapper("mapper")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, projectPath + "\\yeb-generator\\src\\main\\resources\\mapper\\")); // 设置mapperXml生成路径
                })
                .strategyConfig((scanner, builder) -> {
                    builder.addTablePrefix("t_")
                            .addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all")))
                            //数据库表映射到实体的命名策略,数据库表字段映射到实体的命名策略,lombok模型
                            .entityBuilder()
                            .enableLombok()
                            .naming(NamingStrategy.underline_to_camel)
                            .columnNaming(NamingStrategy.no_change)
                            //生成 @RestController 控制器
                            .controllerBuilder()
                            .enableRestStyle()
                            .enableHyphenStyle()
                            //xml数据库表映射到实体的命名策略、数据库表字段映射到实体的命名策略
                            .mapperBuilder()
                            .enableBaseResultMap()
                            .enableBaseColumnList();
                })
                // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
